<?php
namespace Admin\Controller;
use Admin\Controller\AiController;
class AuthController extends AiController {
    function index($auth_name='', $auth_c='',$auth_a='') {
        $where = " 1=1 ";
        if ($auth_name) $where .=" AND auth_name LIKE '%{$auth_name}%' ";
        if ($auth_c) $where .=" AND auth_c LIKE '%{$auth_c}%' ";
        if ($auth_a) $where .=" AND auth_a LIKE '%{$auth_a}%' ";
        $this->assign('auth_name',$auth_name);
        $this->assign('auth_c',$auth_c);
        $this->assign('auth_a',$auth_a);
        $order = " auth_id DESC ";
        import('ORG.Util.Page');
        $mod = D('Auth');
        $count = $mod->where($where)->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count); // 实例化分页类 传入总记录数
        // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
        $Page->parameter = array_map('urlencode', get_page_param($map));
        $nowPage = isset($_GET['p']) ? $_GET['p'] : 1;
        $list = $mod->where($where)
            ->order($order)
            ->page($nowPage . ',' . $Page->listRows)
            ->select();
        $show = $Page->show(); // 分页显示输出
        
        $this->assign('page', $show); // 赋值分页输出
        if($list){
            foreach($list as $key=>&$val){
                $val['auth_name'] = str_repeat('-->',$val['auth_level']).$val['auth_name'];
            }
        }
        $this->assign('list', $list); // 赋值数据集
        $this->display();
    }
    
    public function add(){
        if(IS_POST){
            $auth['auth_name']  = I('auth_name');
            $auth['auth_pid']   = I('auth_pid');
            $admin_name         = I('admin_name','','trim');
            $auth['admin_name'] = $admin_name ? $admin_name : 'Admin';
            $auth['auth_c']     = I('auth_c');
            $auth['auth_a']     = I('auth_a');
            $auth['auth_id']    = I('auth_id');
            $auth['auth_level'] = I('auth_level');
            $res = D('Auth')->addAuth($auth);
            if($res){
                $this->success('编辑成功',U('index'));
            }else{
                $this->error('编辑失败');
            }
        }else{
            $auth_id = I('auth_id');
            if($auth_id){
                //修改
                $res = D('Auth')->where('auth_id='.$auth_id)->find();
                $this->assign('auth_name',  $res['auth_name']);
                $this->assign('auth_c',     $res['auth_c']);
                $this->assign('auth_a',     $res['auth_a']);
                $this->assign('auth_id',    $res['auth_id']);
                $this->assign('auth_level', $res['auth_level']);
                $this->assign('auth_pid1',  $res['auth_pid']);
            }
            $info = $this -> getInfo(true);
            $authinfo = array();
           
            foreach($info as $v){
                $authinfo[$v['auth_id']] = $v['auth_name'];
            }
            foreach($authinfo as $key=>$val){
                $str[]=array('auth_id'=>$key,'auth_name'=>$val);
            }
            $this->assign('auth_pid',$str);
            $this->display('form');
        }
        
    }

    function getInfo($flag=false){
        //如果$flag标志为false，查询全部的权限信息
        //如果$flag标志为true,只查询level=0/1的权限信息
        if($flag == true){
            $info = D('Auth')->where('auth_level<2')->order('auth_path asc')->select();
        }else {
            $info = D('Auth')->order('auth_path asc')->select();
        }
        foreach($info as $k => $v){
            $info[$k]['auth_name'] = str_repeat('-->',$v['auth_level']).$info[$k]['auth_name'];
        }
        return $info;
    }

    //修改方法 
    public function del(){
         $auth_id = I('auth_id');
         //判断是否存在子分类，如果存在则不可以删除
         $auth = D('Auth')->where('auth_pid ='.$auth_id)->select();
         if($auth){
             $this->error('该分类存在子分类，无法删除!',U('Auth/index'));
         }
         $res = D('Auth')->delete($auth_id);
         if($res){
             $this->success('删除成功',U('Auth/index'));
         }else{
             $this->error('删除失败',U('Auth/index'));
         }
    }
}
